package top.jacktgq.itheima.netty.protocol_design.groupchat.message;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import lombok.Data;
import lombok.ToString;
import top.jacktgq.itheima.netty.protocol_design.groupchat.server.session.SessionFactory;

@Data
@ToString(callSuper = true)
public class ChatRequestMessage extends Message {
    private String content;
    private String to;
    private String from;

    public ChatRequestMessage() {
    }

    public ChatRequestMessage(String from, String to, String content) {
        this.from = from;
        this.to = to;
        this.content = content;
    }

    @Override
    public int getMessageType() {
        return ChatRequestMessage;
    }

    @Override
    public void handle(ChannelHandlerContext ctx) {
        Channel channel = SessionFactory.getSession().getChannel(to);
        // 不为空，表示对方在线
        if (channel != null) {
            channel.writeAndFlush(new ChatResponseMessage(from, content));
        } else {
            ctx.writeAndFlush(new ChatResponseMessage(false, "目标用户" + to + "不在线或者不存在"));
        }
    }
}
